package com.vehicle_information_management_system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle_information_management_system.dto.EmployeeCount;
import com.vehicle_information_management_system.mapper.DriverInfoMapper;
import com.vehicle_information_management_system.pojo.DriverInfo;
import com.vehicle_information_management_system.service.impl.DriverInfoServiceImpl;
import com.vehicle_information_management_system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/driverInfo")
public class DriverInfoController {
    @Autowired
    private DriverInfoServiceImpl driverInfoService;

    @RequestMapping("/list")
    public Result<?> listPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        IPage<DriverInfo> driverInfoIPage= driverInfoService.listPage(pageNum,pageSize,search);

        return Result.success(driverInfoIPage);
    }
    @RequestMapping("/add")
    public Result<?> add(@RequestBody DriverInfo driverInfo) {
        try{
            driverInfoService.add(driverInfo);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
        return Result.success("添加成功");
    }
    @RequestMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        try{
            driverInfoService.delete(id);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
        return Result.success("删除成功");
    }
    @RequestMapping("/update")
    public Result<?> update(@RequestBody DriverInfo driverInfo) {
        try{
            driverInfoService.update(driverInfo);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
        return Result.success("修改成功");
    }
    @RequestMapping("/getDriverInfoByDepartment")
    public Result<?> getDriverInfoByDepartment() {
        try{
            List<EmployeeCount> driverInfoList = driverInfoService.getDriverInfoByDepartment();
            return Result.success(driverInfoList);
        }catch (Exception e) {
            return Result.error(400, e.getMessage());
        }
    }
}
